//
//  ShopView.m
//  PetAPP
//
//  Created by dllo on 16/1/24.
//  Copyright © 2016年 Yuming. All rights reserved.
//

#import "ShopView.h"
#import "HeaderOfShop.h"
#import "NetworkingHandle.h"
#import "ModelOfShop.h"
#import "CellOfShop.h"

@interface ShopView ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain)UITableView *tableView;

@property (nonatomic, retain)NSMutableArray *dataArr;

@end

@implementation ShopView

- (void)dealloc {

    [_tableView release];
    [_dataArr release];
    [super dealloc];

}

- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor blackColor];
        [self createSubViewsWithFrame:(CGRect){.size = frame.size}];
        [self handleData];
    }
    
    return self;
}
#pragma mark - 处理数据
- (void)handleData {
    
    [NetworkingHandle handleWorkData:@"http://ts.leepet.com/plugins/API.v1.0/?&a=goods&m=indexTopicGoods" AndBody:@"page=1&pageSize=10" value:@{@"Content-Type" : @"application/x-www-form-urlencoded"} handleBlock:^(id result) {
        
        NSArray *tempArr = [[result objectForKey:@"data"] objectForKey:@"info"];
        self.dataArr = [NSMutableArray array];
        for (NSDictionary *dic in tempArr) {
            ModelOfShop *model = [[ModelOfShop alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.dataArr addObject:model];
            [model release];
            
        }
        [self.tableView reloadData];
    }];
    


}


#pragma mark - 创建头部
- (void)createTableViewOfHeaderView {
    HeaderOfShop *shopView = [[HeaderOfShop alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 285)];
    self.tableView.tableHeaderView = shopView;
    [shopView release];
    

}
#pragma mark - 创建tableview
- (void)createSubViewsWithFrame:(CGRect)frame {
    self.tableView = [[UITableView alloc] initWithFrame:frame style:UITableViewStylePlain];
    [self addSubview:self.tableView];
    [_tableView release];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.tableView registerClass:[CellOfShop class] forCellReuseIdentifier:@"poolOfCell"];
    [self createTableViewOfHeaderView];
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    CellOfShop *cell = [tableView dequeueReusableCellWithIdentifier:@"poolOfCell"];
    ModelOfShop *model = [self.dataArr objectAtIndex:indexPath.row];
    cell.backgroundColor = [UIColor colorWithRed:0.9625 green:0.9625 blue:0.9625 alpha:1.0];
    [cell handleDataWithModel:model];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    
    return cell;

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == self.dataArr.count - 1){
        return 140;
    }else{
        return 150;
    }

}


@end
